(function($) {
	"use strict";
	var _left = "portal_l", _center = "portal_m", _right = "portal_r";

	$.widget("isperp.portal", {
		options : {
			tableCls : "form-list",
			tdCls : "form-text",
			tdCls2 : "single",
			ulCls : "tag-list",
			layCls : "layout-list",
			min : "min",
			mintext : "收起",
			max : "max",
			maxtext : "展开",
			close : "close",
			closetext : "关闭",
			refreshtext : "刷新",
			refresh : "refresh",
			_groupItemContent : "itemContent",
			_groupItemHead : "itemHeader",
			_groupWrapperClass : "groupWrapper",
			_groupItemClass : "groupItem",
			_defaultHeight : "240",
			roleId : "",
			userId : "",
			layout : {
				location : {// 三列容器
					left : _left,
					center : _center,
					right : _right
				},
				locationId : {
					left : "#" + _left,
					center : "#" + _center,
					right : "#" + _right
				},
				layoutCss : {
					0 : "1:3",
					1 : "3:1",
					2 : "2:2",
					3 : "1:2:1",
					4 : "1:1:2",
					5 : "2:1:1",
					6 : "1:1:1"
				},
				layoutText : {
					0 : "w250 w750 wnone",
					1 : "w750 w250 wnone",
					2 : "w500 w500 wnone",
					3 : "w250 w500 w250",
					4 : "w250 w250 w500",
					5 : "w500 w250 w250",
					6 : "w330 w330 w330"
				}
			},
			data : {}
		},
		utils : {
			format : function(str, model) {// 格式化指定键值的模板
				for ( var k in model) {
					var re = new RegExp("{" + k + "}", "g");
					str = str.replace(re, model[k])
				}
				return str
			},
			refresh : function(layout) {// 刷新3个布局
				$("#" + layout.left, "#" + layout.center, "#" + layout.right).sortable('refresh');
			}

		},
		_create : function() {
			var self = this, o = self.options, element = self.element;

			var _box = "<div id='portal'></div>", _template = {// 模板
				l : "<div id='" + o.layout.location.left + "' class='" + o._groupWrapperClass + " w250'/>",
				m : "<div id='" + o.layout.location.center + "' class='" + o._groupWrapperClass + " w500'/>",
				r : "<div id='" + o.layout.location.right + "' class='" + o._groupWrapperClass + " w250'/>",
				portalWrap : "<div id='widget_{key}' data-id='{key}' style='height:{height}' class='" + o._groupItemClass + "'/>",
				itemHeader : "<div class='" + o._groupItemHead + "'><h3 class='headerTitle'>{iconImg}<span style=''>{name}</span></h3></div>",
				itemContent : "<div class='" + o._groupItemContent + "'/>"
			};
			self.box = $(_box);
			self._elements = {};

			self._elements.m_l = $(_template.l);
			self._elements.m_m = $(_template.m);
			self._elements.m_r = $(_template.r);
			self.box.append(self._elements.m_l);
			self.box.append(self._elements.m_m);
			self.box.append(self._elements.m_r);

			$(o.data).each(function(index, item) {
				var w = self._createPortalOne(item, self.options, _template);
				var lindex = (item.orderNum) % 3;
				if (lindex == 0) {
					self._elements.m_l.append(w);
				} else if (lindex == 1) {
					self._elements.m_m.append(w);
				} else {
					self._elements.m_r.append(w);
				}

			});

			element.append(self.box);

			self._eventSortable();
			self._eventRefresh();
		},
		destroy : function() {
			$.Widget.prototype.destroy.call(this);
		},

		_createPortalOne : function(item, options, _template) {// 创建单个portal
			// item
			var me = this;
			var itemHeader = me._createItemHeader(options, _template, item.title, item.iconImg), // header
			itemContent = me._createItemContent(_template, item.contentUrl, (item.height || options._defaultHeight) - 37), // content

			portalWrap = $(this.utils.format(_template.portalWrap, {
				"key" : item.id,
				"height" : (item.height || options._defaultHeight) + "px"
			})).append(itemHeader).append(itemContent);

			return portalWrap;
		},

		_createItemHeader : function(options, _template, title, iconImg) {// 创建Head
			var me = this;
			var _itemHeader = $(this.utils.format(_template.itemHeader, {
				"name" : title,
				"iconImg" : iconImg ? "<img src='" + iconImg + "'>" : ""
			})), // 格式化header
			_actionWrap = me._createDiv("action").hide().appendTo(_itemHeader);

			me._createA(options.refresh, options.refreshtext, true).appendTo(_actionWrap);

			_itemHeader.hover(function() {// 滑过标题出现删除图标
				$(this).find(".action").show();
			}, function() {
				$(this).find(".action").hide();
			});
			return _itemHeader;
		},
		_createItemContent : function(_template, contentUrl, height) {// 创建content
			var _content = $(_template.itemContent);
			$("<iframe src='" + contentUrl + "' style='width:99.9%;height:" + height + "px;border:none;' frameborder='0'></iframe>").appendTo(_content);
			return _content;
		},
		_createA : function(classname, title, isShow) {// 创建A
			var _a = $("<a href='javascript:void(0);' class='" + classname + "' title='" + title + "'/>");
			if (!isShow) {
				_a.hide();
			}
			return _a;
		},
		_createDiv : function(classname) {
			var _div = $("<div/>").addClass(classname);
			return _div;
		},
		_eventSortable : function() {// 绑定排序
			var o = this.options;
			$("." + o._groupWrapperClass).sortable({
				connectWith : "." + o._groupWrapperClass,
				opacity : "0.6",
				dropOnEmpty : true,
				update : function(event, ui) {
					if (this === ui.item.parent()[0]) {
						// 提交跟新结果
						var ps = [], vs = [];
						$("#portal_l").children(".groupItem").each(function(index, item) {
							var id = $(this).data("id");
							var tindex = 3 * index;
							ps.push(id);
							vs.push(tindex);
						});
						$("#portal_m").children(".groupItem").each(function(index, item) {
							var id = $(this).data("id");
							var tindex = 3 * index + 1;
							ps.push(id);
							vs.push(tindex);
						});
						$("#portal_r").children(".groupItem").each(function(index, item) {
							var id = $(this).data("id");
							var tindex = 3 * index + 2;
							ps.push(id);
							vs.push(tindex);
						});

						if (o.roleId && o.roleId != "") {
							$.ajax({
								url : "app/portal/saveRolePortalConfig",
								type : "post",
								traditional : true,
								data : {
									"protals" : ps,
									"indexs" : vs,
									"roleId" : o.roleId
								}

							});
						} else if (o.userId && o.userId != "") {
							$.ajax({
								url : "app/portal/savePersonPortalConfig",
								type : "post",
								traditional : true,
								data : {
									"protals" : ps,
									"indexs" : vs
								}

							});
						}

					}

				}
			}).disableSelection();
		},
		_eventRefresh : function() {
			var o = this.options;
			$("." + o.refresh).on("click", function() {// 刷新
				var $t = $(this).parents(".groupItem").find("iframe");
				$t.attr("src", $t.attr("src"));
			});
		}

	});
}(jQuery));
